<?php
class property extends ujn_controller {
	var $main_url = '';
	var $login_url = '';
	var $uploaddir = '';
	
	function _before() {
		$this->main_url = ujn::getCFG('plat.url.main_url');
		$this->login_url = ujn::getCFG('plat.url.login_url');
		$this->cdn_url = ujn::getCFG('plat.url.cdn_url');
		
		$tmp_url = WWWROOT . 'p_admin_plugins/property_manage/_tmp/' . PLATFORM . '/'; //local
		$this->assign(array(
			'tmp_url' => $tmp_url,
			//'tmp_url' => $this->cdn_url,
		));
	}
	
	public function lists() {
		$params = array( 'p' => 'facebook' );
		$url = $this->main_url . '_hookdoor/_datadoor.php?property-lists';
		$x = tls_func::rest_request('POST', $url, null, $params, false, true);
		
		$items = $x['data']['flag'] ? $x['data']['lists'] : array();
		
		$this->assign(array(
			'items'	=> $items,
		));
	}
	
	public function add() {
		if( isset( $_POST['form_id'] ) ) {
			$params = $_POST;
			$params['p'] = 'facebook';
			$params['flag'] = 0; //添加
			$url = $this->main_url . '_hookdoor/_datadoor.php?property-edit';
			$x = tls_func::rest_request('POST', $url, null, $params, false, true);
			
			if( $x['data']['flag'] ) {
				$this->redirect('?rooms-lists');
			}
			else{
				echo '添加失败!';exit;
			}
		}
	}
	
	public function edit(){
		$proid = (isset($_REQUEST['proid']) && !empty($_REQUEST['proid'])) ? $_REQUEST['proid'] : '0';
		
		if( isset( $_POST['form_id'] ) ) {
			$params = $_POST;
			$params['p'] = 'facebook';
			$url = $this->main_url . '_hookdoor/_datadoor.php?property-edit';
			
			if( !empty( $_FILES['file']['name'] ) ){ 
				$params['file'] = $this->uploadFile();
			}
			
			$x = tls_func::rest_request('POST', $url, null, $params, false, true);
			
			if( $x['data']['flag'] ) {
				$this->redirect('?property-lists');
			}
			else{
				echo '修改失败!';exit;
			}
		}
		
		if( !empty($proid) ){
			$params = array( 'p' => 'facebook', 'proid' => $proid );
			$url = $this->main_url . '_hookdoor/_datadoor.php?property-get';
			$x = tls_func::rest_request('POST', $url, null, $params, false, true);
			$item = $x['data']['lists'];
			$this->assign(array(
				'item'	=> $item,
				'cmd' => $cmd,
			));
		}
	}
	
	protected function uploadFile() {
		$uploaddir = PLUGINROOT . '_tmp/' . PLATFORM . '/';
		
		list($name, $suffix) = explode('.', $_FILES['file']['name']);
		$file = md5($name . NOW) . '.' . $suffix;
		
		$uploadfile = $uploaddir . $file;
		
		if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
		    return $file;
		}
		
		return false;
	}
	
	public function rsynctocdn(){
		$c = ujn::getCFG('plat.rsync');
		$cmd = '';
		
		if( $_SERVER['HTTP_HOST'] == '50.22.168.2' ){
			$cmd = "rsync --progress -avz --exclude=.svn {$c['localdir']}* --rsh=\"ssh -i {$c['localkey']} -p{$c['remoteport']}\" sjh@{$c['remotehost']}:{$c['remotedir']}";
			
			$this->assign(array(
				'cmd'	=> $cmd,
			));
		}
	}
}